package com.hoas.databox.spring.autoconfigure;

import com.hoas.databox.core.DataRepostroy;
import com.hoas.databox.core.EasyDataBox;
import com.hoas.databox.redis.RedisDataRepostroy;
import com.hoas.databox.spring.SpringRedisDataRepostroy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPool;

@Configuration
public class EasyDataBoxAutoConfigure {

    @Bean
    @ConditionalOnProperty(prefix="databox.easybox",name = "namespace", matchIfMissing = false)
    public EasyDataBoxProperties easyDataBoxProperties() {
        return new EasyDataBoxProperties();
    }

    @Bean
    @ConditionalOnMissingBean(DataRepostroy.class)
    @ConditionalOnClass(RedisTemplate.class)
    @ConditionalOnProperty(value = "databox.easybox.data-repostroy", havingValue="spring-redis")
    public DataRepostroy springRedisRepostroy(RedisTemplate redisTemplate){
        return new SpringRedisDataRepostroy( redisTemplate );
    }

    @Bean
    @ConditionalOnMissingBean(DataRepostroy.class)
    @ConditionalOnClass(JedisPool.class)
    @ConditionalOnProperty(value = "databox.easybox.data-repostroy", havingValue="redis", matchIfMissing = true)
    public DataRepostroy redisRepostroy(JedisPool jedisPool){
        return new RedisDataRepostroy( jedisPool );
    }

    @Bean
    @ConditionalOnBean(DataRepostroy.class)
    public EasyDataBox easyDataBox(EasyDataBoxProperties properties, DataRepostroy dataRepostroy) {
        EasyDataBox dataBox = new EasyDataBox( properties.getNamespace(), dataRepostroy);
        return  dataBox;
    }


}
